import { computed, ref, readonly } from 'vue'
import BasicInfo from '@/api/client/basicInfo.js'
import store from '@/store'
const basicInfo = {}
basicInfo.loaded = ref(false)
basicInfo.raw = ref({})
basicInfo.transformed = ref({})
export function useBasicInfo(id, resolve = () => {}, noCache = false) {
  if (!basicInfo.loaded.value || noCache) {
    getData(id, resolve)
  } else {
    resolve({
      raw: readonly(basicInfo.raw),
      transformed: readonly(basicInfo.transformed)
    })
  }
  const reload = getData.bind(this, id, resolve)
  return {
    basicInfo: {
      raw: readonly(basicInfo.raw),
      transformed: readonly(basicInfo.transformed),
      loaded: readonly(basicInfo.loaded)
    },
    reload
  }
}
function getData(id, resolve) {
  BasicInfo.getInfo(id).then(async res => {
    basicInfo.raw.value = res.data
    const dict = ref(computed(() => store.state.dict))
    await store.dispatch('dict/getClientTypes')
    await store.dispatch('dict/getCountriesOrRegions')
    await store.dispatch('dict/getOrganizations', true)
    await store.dispatch('dict/getTimeZones')
    await store.dispatch('dict/getSalesBUs')
    await store.dispatch('dict/getOwnersBUs', id)
    await store.dispatch('dict/getOperateBUs')
    await store.dispatch('dict/getBusinessAreas')
    await store.dispatch('dict/getClientTiers')
    await store.dispatch('dict/getClientStatusTypes')
    await store.dispatch('dict/getCurrencies')
    await store.dispatch('dict/getAllHrSalesBUs')
    basicInfo.loaded.value = true
    resolve({
      raw: readonly(basicInfo.raw),
      transformed: readonly(basicInfo.transformed)
    })
    dict.value.clientTypes.forEach(clientType => {
      if (clientType.value === basicInfo.raw.value.clientKind) {
        basicInfo.transformed.value.clientKind = clientType.text
      }
    })
    dict.value.countriesOrRegions.forEach(countryOrRegion => {
      if (countryOrRegion.value === basicInfo.raw.value.countryID) {
        basicInfo.transformed.value.countryID = countryOrRegion.text
      }
    })
    dict.value.organizations.forEach(organization => {
      if (organization.value === basicInfo.raw.value.parentID) {
        basicInfo.transformed.value.parentID = organization.text
      }
    })
    dict.value.timeZones.forEach(timeZone => {
      if (timeZone.value === basicInfo.raw.value.timeZone) {
        basicInfo.transformed.value.timeZone = timeZone.text
      }
    })
    dict.value.salesBUs.forEach(bu => {
      if (bu.value === basicInfo.raw.value.saleBUID) {
        basicInfo.transformed.value.saleBUID = bu.text
      }
    })
    dict.value.allHrSalesBUs.forEach(bu => {
      if (bu.value === basicInfo.raw.value.hrSalesBUID) {
        basicInfo.transformed.value.hrSalesBUID = bu.text
      }
    })
    dict.value.operateBUs.forEach(bu => {
      if (bu.value === basicInfo.raw.value.operateBU) {
        basicInfo.transformed.value.operateBU = bu.text
      }
    })
    dict.value.businessAreas.forEach(businessArea => {
      if (businessArea.value === basicInfo.raw.value.businessDirection) {
        basicInfo.transformed.value.businessDirection = businessArea.text
      }
    })
    dict.value.clientTiers.forEach(clientTier => {
      if (clientTier.value === basicInfo.raw.value.clientTier) {
        basicInfo.transformed.value.clientTier = clientTier.text
      }
    })
    dict.value.clientStatusTypes.forEach(type => {
      if (type.value === basicInfo.raw.value.status) {
        basicInfo.transformed.value.status = type.text
      }
    })
    dict.value.currencies.forEach(currency => {
      if (currency.value === basicInfo.raw.value.currencyID) {
        basicInfo.transformed.value.currencyID = currency.text
      }
    })
  })
}
